package org.dydl.dao.system.repository;

import org.dydl.model.system.Orders;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.CrudRepository;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

@Repository
public interface IOrderRepository extends CrudRepository<Orders, Integer>,
		JpaSpecificationExecutor<Orders> {

	@Query(value = "FROM Orders ord ORDER BY ord.createDate DESC", countQuery = "SELECT count(ord.id) FROM Orders ord ORDER BY ord.createDate DESC")
	Page<Orders> getOrdersList(Pageable pageable);

	@Query(value = "FROM Orders ord WHERE ord.orderNo LIKE :orderNo ORDER BY ord.createDate DESC", countQuery = "SELECT count(ord.id) FROM Orders ord WHERE ord.orderNo LIKE :orderNo ORDER BY ord.createDate DESC" )
	Page<Orders> getOrdersListLike(@Param("orderNo") String orderNo,
			Pageable pageable);

	@Modifying
	@Transactional
	@Query(value = "UPDATE orders set EXAMINE_STATUS = 1 WHERE ID = :id", nativeQuery = true)
	void changeExamineStatus(@Param("id") int id);
	
	@Modifying
	@Query(value = "UPDATE orders set PRODUCTION_INSTRUCTION_STATUS = 1,YD_GOODS_YIELD_STATUS = 1 WHERE ORDER_NO = :orderNo", nativeQuery = true)
	void changeProductionInstructionStatus(@Param("orderNo") String orderNo);
	
	@Modifying
	@Query(value = "UPDATE orders set DELIVERIES_STATUS = 1 WHERE ORDER_NO = :orderNo", nativeQuery = true)
	void changeDeliveriesStatus(@Param("orderNo")String orderNo);
	
	@Query(value = "SELECT * FROM orders WHERE ORDER_NO = :orderNo", nativeQuery = true)
	Orders findByOrderNo(@Param("orderNo") String orderNo);

	@Query(value = "SELECT UUID FROM orders WHERE ORDER_NO = :orderNo", nativeQuery = true)
	String findUUIDByOrderNo(@Param("orderNo") String orderNo);
	

}
